﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Principal;

namespace abletonlogreader {
    public partial class frmMain : Form {
       
        public frmMain() {
            InitializeComponent();
        }

        private void read_file_into_text_box(string file, TextBox target) {

            try {
                using (StreamReader sr = new StreamReader(file)) {
                    target.Text = "";

                    while (!sr.EndOfStream) {
                        string line = sr.ReadLine();
                        if (target == this.txtAbletonLog) {
                            if ((line.IndexOf("Exception") != -1) || (line.IndexOf("RemoteScriptError:") != -1)) {
                                target.Text += line + System.Environment.NewLine;
                            }
                        }
                        else {
                            target.Text += line + System.Environment.NewLine;
                        }
                    }
                }
            }
            catch {
            }

        }

        private void clear_ableton_log() {
            using (StreamWriter sw = new StreamWriter(Constants.ableton_log_file)) {
                sw.Write("");
            }
                    
        }

        private void clear_custom_log() {
            using (StreamWriter sw = new StreamWriter(Constants.ableton_log_file)) {
                sw.Write("");
            }
        }

        private void tabDocs_KeyUp(object sender, KeyEventArgs e) {

            if (e.KeyCode == Keys.F5) {
                this.read_file_into_text_box(Constants.ableton_log_file, this.txtAbletonLog);
                this.read_file_into_text_box(Constants.custom_log_file, this.txtCustomLog);

            }

            if (e.KeyCode == Keys.F6) {
                this.clear_ableton_log();
                this.txtAbletonLog.Text = "";
                
                this.clear_custom_log();
                this.txtCustomLog.Text = "";
            }

            if (e.KeyCode == Keys.F7) {
                this.tabDocs.SelectTab(0);
            }

            if (e.KeyCode == Keys.F8) {
                this.tabDocs.SelectTab(1);
            }
        }

        private void frmMain_Resize(object sender, EventArgs e) {
            
            this.tabDocs.Width = this.Width - 14;
            this.tabDocs.Height = this.Height - 34;

            this.txtAbletonLog.Width = this.tabDocs.Width - 18;
            this.txtAbletonLog.Height = this.tabDocs.Height - 39;

            this.txtCustomLog.Width = this.tabDocs.Width - 18;
            this.txtCustomLog.Height = this.tabDocs.Height - 39;
        }

    }

    public class Constants {
        public static String username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString().Substring(System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString().LastIndexOf("\\") + 1);
        public static String ableton_log_file = "C:\\Documents and Settings\\" + Constants.username + "\\Application Data\\Ableton\\Live 8.2.2\\Preferences\\Log.txt";
        public static String custom_log_file = "C:\\log";
    }
}
